package com.sh.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.Executors;  
import java.util.concurrent.ScheduledExecutorService;  
import java.util.concurrent.TimeUnit;  

import org.eclipse.paho.client.mqttv3.MqttClient;  
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;  
import org.eclipse.paho.client.mqttv3.MqttException;  
import org.eclipse.paho.client.mqttv3.MqttSecurityException;  
import org.eclipse.paho.client.mqttv3.MqttTopic;  
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

import com.sh.mqtt.PushCallback;

/**
* @comments
* @author zg
* @date 2018年3月14日
*/
public class HardwareMocker {
	
    public static final String SERVER_HOST = "127.0.0.1";  
    public static final int SERVER_PORT = 8081;  
 
   
    public static void main(String[] args) throws Exception {     
    	String bindingINfo = "12345678,ABCDEF";
    	sendBinding(bindingINfo);
    }  
    
    
    public static void sendBinding(String sendContent) throws Exception {
    	sendContent = sendContent + "\n";
    	 Socket socket = new Socket(SERVER_HOST, SERVER_PORT);  
         if (socket.isConnected()){  
             OutputStream os =null;  
             InputStream is = null;  
             os = socket.getOutputStream();  
             os.write(sendContent.getBytes("UTF-8")); 
             os.flush(); 
             while (true) {  
                 try {  
                     is = socket.getInputStream();  
                     byte[] resp = new byte[256];  
                     is.read(resp);  
                     System.out.println("<<<<<<<<<收到服务器消息:" + new String(resp));  
                 } catch (Exception e) {  
                     e.printStackTrace();  
                     if(os != null)  
                         os.close(); //由于是长连接，抛异常时要关闭os，否则会broken pipe  
                     if(is != null)  
                         is.close();  
                 }  
                   
             }  
         }  
    }
    
}
